package com.example.shop_2304.login

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.shop_2304.R
import com.example.shop_2304.databinding.ActivityUpdateBinding
@Route(path = "/app/UpdateActivity")
class UpdateActivity : BaseActivity<ActivityUpdateBinding,LoginViewModel>() {
    override fun initView() {
        val phone = SPUtils.getInstance().getString("phone")
        val replaceRange = phone.replaceRange(3, 7, "****")
        binding.tvPhone.text = "当前手机号:$replaceRange"
        //发送验证码
        binding.btnCode.setOnClickListener {
            viewModel.codeSend(phone)
        }
        //点击按钮，实现确认修改密码
        binding.btnUpdate.setOnClickListener {
            val etPwd = binding.etPwd.text.toString()
            val etRepwd = binding.etRepwd.text.toString()
            val etCode = binding.etCode.text.toString()
            if(etPwd.isEmpty()||etRepwd.isEmpty()||etCode.isEmpty()){
                ToastUtils.showLong("所有输入框均不能为空")
                return@setOnClickListener
            }
            if(etPwd!=etRepwd){
                ToastUtils.showLong("两次密码不一致")
                return@setOnClickListener
            }
            viewModel.updatePwd(phone,etCode,etPwd)
        }
    }

    override fun initData() {
        viewModel.codeSendSuccess.observe(this){
            if(it.code==200){
                binding.etCode.setText(it.data.content.substring(8,14))
            }
        }
        viewModel.updatePwdSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                finish()
            }
        }
    }

    override fun getViewModel(): Class<LoginViewModel> = LoginViewModel::class.java

    override fun getLayout(): Int = R.layout.activity_update

}